﻿using RBAC.Domain;
using RBAC.Domain.Dto;
using RBAC.Server.Interface;
using Sowrk03_28.Repository;
using Sowrk03_28.Repository.Interface;
using Swork03_28.Domain.RBAC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.Server.Impls
{
    public class MyRoleServer : IRoleServer
    {
        private readonly IRoleRepository Roles;
        private readonly IRepository<UserRole> UserRole;
        private readonly IRepository<UserInfo> UserInfo;

        public MyRoleServer(IRoleRepository roles,
            IRepository<UserRole> userRole,
            IRepository<UserInfo> userInfo)
        {
            Roles = roles;
            UserRole = userRole;
            UserInfo = userInfo;
        }

        public async Task<int> CreateRole(Roles role)
        {
            var info = Roles.GetValues(x => x.Name == role.Name).Result.FirstOrDefault();
            role.CreatePeople = "张三";
            role.CreateTime = DateTime.Now;
            role.UpdPeople = null;
            role.UpdTime = null;
            if (info == null)
            {
                await Roles.CreateAsync(role);
                return 1;
            }
            else
            {
                //角色名称已存在
                return 0;
            }
        }

        public async Task<(int totalCount, int pageCount, List<Roles> list)> GetRoles(int pageIndex, int pageSize)
        {
            var info = Roles.GetValues(x => !x.IsDel).Result.ToList();
            int totalCount = info.Count;
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
            info = info.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return (totalCount, pageCount, info);
        }

        public async Task<int> Del(int id)
        {
            var info = await Roles.GetAsync(id);
            info.IsDel = true;
            await Roles.UpdateAsync(info);
            return 1;
        }

        public async Task<Roles> GetRolesUpd(int id)
        {
            return await Roles.GetAsync(id);
        }

        public async Task<int> Update(Roles role)
        {
            var only = Roles.GetValues(x => x.Name == role.Name && x.Id != role.Id).Result.FirstOrDefault();
            if (only != null)
            {
                return 0;
            }
            else
            {
                role.UpdPeople = "李四";
                role.UpdTime = DateTime.Now;
                await Roles.UpdateAsync(role);
                return 1;
            }
        }

        public async Task<List<MenuItem>> GetMenuItem(int SectorId)
        {
            var info = await UserInfo.AllAsync();
            var res = new List<MenuItem>();
            if (SectorId != 0)
            {
                info = info.Where(x => x.SectorId == SectorId).ToList();
                foreach (var item in info)
                {
                    res.Add(new MenuItem
                    {
                        label = item.Name,
                        value = item.Id
                    });
                }
                return res.ToList();
            }
            else
            {
                foreach (var item in info)
                {
                    res.Add(new MenuItem
                    {
                        label = item.Name,
                        value = item.Id
                    });
                }
                return res.ToList();
            }
        }

        public async Task<int> AddRole(List<MenuItem> userId, int roleId, int SectorId)
        {
            var a = 0;
            if (SectorId != 0)
            {
                var dels = UserInfo.GetValues(x => userId.Select(x => x.value).Contains(x.Id)).Result.Select(x => x.SectorId).ToArray();
                if (dels.Contains(SectorId))
                {
                    var sector = UserInfo.GetValues(x => x.SectorId == SectorId).Result.Select(x => x.Id).ToArray();
                    var res = UserRole.GetValues(x => x.RoleId == roleId && sector.Contains(x.UserId)).Result.ToList();
                    await UserRole.DeleteAllAsync(res);
                }
            }
            else
            {
                var dels = UserRole.GetValues(x => x.RoleId == roleId).Result.ToList();
                await UserRole.DeleteAllAsync(dels);
            }
            foreach (var item in userId)
            {
                var only = UserRole.GetValues(x => x.RoleId == roleId && x.UserId == item.value).Result.FirstOrDefault();
                if (only != null)
                {
                    a += 1;
                    if (a == userId.Count)
                    {
                        return -1;
                    }
                    continue;
                }
                else
                {
                    if (item.value == 0)
                    {
                        continue;
                    }
                    var info = new UserRole
                    {
                        RoleId = roleId,
                        UserId = item.value
                    };
                    await UserRole.CreateAsync(info);
                }
            }
            return 1;
        }

        public async Task<List<UserDto>> GetUserInfos(int roleId)
        {
            var userId = UserRole.GetValues(x => x.RoleId == roleId).Result.ToArray();
            var result = new List<UserDto>();
            foreach (var item in userId)
            {
                if (item.UserId == 0)
                {
                    continue;
                }
                var userinfo = await UserInfo.GetAsync(item.UserId);
                result.Add(new UserDto
                {
                    Id = userinfo.Id,
                    Name = userinfo.Name
                });
            }

            return result;
        }

        public async Task<int> DelRoleUser(int roleId, int userId)
        {
            var info = UserRole.GetValues(x => x.RoleId == roleId && x.UserId == userId).Result.FirstOrDefault();
            if (info != null)
            {
                await UserRole.DeleteAsync(info);
            }
            return 1;
        }

        public async Task<List<MenuItem>> UpdUserRole(int RoleId)
        {
            var info = UserRole.GetValues(x => x.RoleId == RoleId).Result.ToArray();
            var res = new List<MenuItem>();
            foreach (var item in info)
            {
                var user = await UserInfo.GetAsync(item.UserId);
                res.Add(new MenuItem
                {
                    label = user.Name,
                    value = item.UserId
                });
            }
            return res;
        }

        public async Task<List<MenuItem>> UpdUserRoleAsDoor(int dId)
        {
            var door = UserInfo.GetValues(x => x.SectorId == dId).Result.Select(x => x.Id).ToArray();
            var info = UserRole.GetValues(x => door.Contains(x.UserId)).Result.ToArray();
            var res = new List<MenuItem>();
            foreach (var item in info)
            {
                var user = await UserInfo.GetAsync(item.UserId);
                res.Add(new MenuItem
                {
                    label = user.Name,
                    value = item.UserId
                });
            }
            return res;
        }
    }
}
